//
//  BaseTabBarController.swift
//  SwiftExercise
//
//  Created by 罗宗庆 on 2021/6/23.
//

import UIKit
import EZSwiftExtensions

class BaseTabBarController: UITabBarController {

    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.lightGray], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.black], for: .selected)
        
        
        
        let v1 = LZQ_WechatVC()
        
        let nav1 = ZXNavigationBarNavigationController(rootViewController: v1)
        
        let v2 = LZQ_ContactVC()
        let nav2 = ZXNavigationBarNavigationController(rootViewController: v2)
        
        let v3 = LZQ_DiscoverVC()
        let nav3 = ZXNavigationBarNavigationController(rootViewController: v3)
        
        
        let v4 = LZQ_ProfileVC()
        let nav4 = ZXNavigationBarNavigationController(rootViewController: v4)
        
  
        self.viewControllers = [nav1,nav2,nav3,nav4]
        
        //添加动画
        self.addLottieView()
        self.tabBar.animationLottieImage(index: 0)
        self.tabBar.unselectedItemTintColor = UIColor.lightGray
        
        v1.title = "直播"
        v2.title = "附近"
        v3.title = "消息"
        v4.title = "我的"
        
        
    }

    
    func addLottieView(){
        self.tabBar.addLottieImage(index: 0, lottieImage: "tab_home_animate")
        self.tabBar.addLottieImage(index: 1, lottieImage: "tab_search_animate")
        self.tabBar.addLottieImage(index: 2, lottieImage: "tab_message_animate")
        self.tabBar.addLottieImage(index: 3, lottieImage: "tab_me_animate")
    }
    func lottieImagePlay(controller:UIViewController){
        let lottieView:LOTAnimationView = controller.view.viewWithTag(100) as! LOTAnimationView
        lottieView.animationProgress = 0
//        lottieView.loopAnimation = false
        lottieView.play()
    }
}



extension BaseTabBarController:UITabBarControllerDelegate{
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        self.lottieImagePlay(controller: viewController)
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        if ((self.tabBar.items?.contains(item)) != nil) {
            let index = self.tabBar.items?.index(of: item)
//            let index = self.tabBar.items?.indexes(of: item)
            self.tabBar.animationLottieImage(index: Int(index!))
        }
    }
}
